#include <system/application.h>
#include <system/platform.h>

namespace fort
{

Application::Application(Platform& platform) :
	platform(platform),
	running(true)
{ 
}

Application::~Application()
{}

void Application::Run()
{
	//-- Initialisation function
	Init();
	
	//-- Aim for 60fps
	const float kTargetFrameTime = 1.0f / 60.0f;
	const float kTooLargeFrameTime = 1.0f / 10.f;
	
	//-- Initlialise previous_frame_time but use
	//-- kTargetFrameTime as this is the first 
	//-- frame and there is no previous time
	float previous_frame_time = platform.GetFrameTime();
	previous_frame_time = kTargetFrameTime;

	while(running)
	{
		//-- Update platform specific functions 
		running = platform.Update();
		if(running)
		{
			//-- Check to see if frame time is too large
			//-- if it is we need use the target frame rate 
			//-- as using a large frame time compound any frame rate drop
			if(previous_frame_time > kTooLargeFrameTime)
				previous_frame_time = kTargetFrameTime;

			//-- Update function
			running = Update(previous_frame_time);

			//-- Render function
			platform.PreRender();
			Render();
			platform.PostRender();

			previous_frame_time = platform.GetFrameTime();
		}
	}

	//-- Cleanup function
	CleanUp();
}
}